package sa.com.sure.ejtimaa;

import java.io.File;
import java.io.FilenameFilter;
import java.text.ParseException;

import sa.com.sure.ejtimaa.R;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.components.MeetingApologistsListComponent;
import sa.com.sure.ejtimaa.components.MeetingAttachmentsListComponent;
import sa.com.sure.ejtimaa.components.MeetingAttendanceListComponent;
import sa.com.sure.ejtimaa.components.MeetingDetailComponent;
import sa.com.sure.ejtimaa.components.MeetingItemsListComponent;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.SQLException;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;



public class MeetingDetialActivity extends Activity {
	
	private MeetingDetailComponent meetingDetailComponent  = null;
	private MeetingItemsListComponent meetingItemsListComponent = null;
	private MeetingAttachmentsListComponent meetingAttachmentsListComponent = null;
	private MeetingAttendanceListComponent meetingAttendanceListComponent = null;
	private MeetingApologistsListComponent meetingApologistsListComponent = null;
	
	private String[] mFileList;
	private File mPath = new File(Environment.getExternalStorageDirectory() + "//");
	private static final String FTYPE = ".txt";    
	private static final int DIALOG_LOAD_FILE = 1000;
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {  
	    try
	    {
	        super.onCreate(savedInstanceState);	     
	        
	        setContentView(R.layout.meeting_detail);
	        
		}
		catch(Exception ex){    		
	    	Helper.HandleError(ex,true);
		}              
    }
        
    @Override
    protected void onStart(){
    	
    	try
	    {
    		super.onStart();    		  
    		
    		meetingDetailComponent  =(MeetingDetailComponent) findViewById(R.id.meetingDetailComponent);
    		meetingItemsListComponent  =(MeetingItemsListComponent) findViewById(R.id.meetingItemsListComponent);
    		meetingAttachmentsListComponent  =(MeetingAttachmentsListComponent) findViewById(R.id.meetingAttachmentsListComponent);
    		meetingAttendanceListComponent =(MeetingAttendanceListComponent) findViewById(R.id.meetingAttendanceListComponent);
    		meetingApologistsListComponent =(MeetingApologistsListComponent) findViewById(R.id.meetingApologistsListComponent);
    		
    		Bundle extras = getIntent().getExtras();
    		MeetingInfo meeting = (MeetingInfo)extras.getSerializable("item");
    		SetData(meeting);
    		
    		loadFileList();
		}
		catch(Exception ex){    		
	    	Helper.HandleError(ex,true);
		}  
    }
            
	public void SetData(MeetingInfo item) throws SQLException, ParseException{
		
		meetingDetailComponent.SetData(item);
		meetingItemsListComponent.SetData(item);
		meetingAttachmentsListComponent.SetData(item);
		meetingAttendanceListComponent.SetData(item);
		meetingApologistsListComponent.SetData(item);
	}
	
	 @Override
	 protected Dialog onCreateDialog(int id){
		  Dialog dialog = null;
		  AlertDialog.Builder builder = new Builder(this);

		  switch(id){
		  case DIALOG_LOAD_FILE:
		   builder.setTitle("Choose your file");
		   if(mFileList == null){
		     Log.e("", "Showing file picker before loading the file list");
		     dialog = builder.create();
		     return dialog;
		   }
		     builder.setItems(mFileList, new DialogInterface.OnClickListener(){
		       public void onClick(DialogInterface dialog, int which){
		          //mChosenFile = mFileList[which];
		          //you can do stuff with the file here too
		       }
		      });
		  break;
		  }
		  dialog = builder.show();
		  return dialog;
		 }
	 
	 private void loadFileList(){
		  try{
		     mPath.mkdirs();
		  }
		  catch(SecurityException e){
		     Log.e("", "unable to write on the sd card " + e.toString());
		  }
		  if(mPath.exists()){
		     FilenameFilter filter = new FilenameFilter(){
		         public boolean accept(File dir, String filename){
		             File sel = new File(dir, filename);
		             return filename.contains(FTYPE) || sel.isDirectory();
		         }
		     };
		     mFileList = mPath.list(filter);
		  }
		  else{
		    mFileList= new String[0];
		  }
		}
}